const db = require("../../config/db");

module.exports = {
    //收藏
    addBookmark({ user_id, news_id, news_category, news_tag, news_title, news_img, news_time, news_media, user_avators }) {
        return new Promise((resolve, reject) => {
            let check_bookmark_sql = `select * from db_bookmark where user_id="${user_id}" and news_id="${news_id}"`;
            db.query(check_bookmark_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: 111,
                        err,
                    });
                } else {
                    if (rows.length == 0) {
                        let insert_bookmark_sql = `insert into db_bookmark(user_id,news_id,news_category,news_tag,news_title,news_img,news_time,news_media,user_avators) values("${user_id}","${news_id}","${news_category}","${news_tag}","${news_title}","${news_img}","${news_time}","${news_media}","${user_avators}")`;
                        db.query(insert_bookmark_sql, (error, result) => {
                            if (error) {
                                reject({
                                    code: 222,
                                    error,
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "收藏成功",
                                });
                            }
                        });
                    } else {
                        resolve({
                            code: 201,
                            msg: "此新闻已经收藏过了",
                        });
                    }
                }
            });
        });
    },

    //取消收藏
    removeBookmark({ user_id, news_id }) {
        return new Promise((resolve, reject) => {
            let check_bookmark_sql = `select * from db_bookmark where user_id="${user_id}" and news_id="${news_id}"`;
            db.query(check_bookmark_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    let remove_bookmark_sql = `DELETE FROM db_bookmark WHERE user_id="${user_id}" and news_id="${news_id}"`;
                    db.query(remove_bookmark_sql, (error, result) => {
                        if (error) {
                            reject(err);
                        } else {
                            resolve({
                                code: 200,
                                msg: "取消收藏成功",
                            });
                        }
                    });
                }
            });
        });
    },

    //获取收藏
    getBookmark({ user_id, news_id }) {
        return new Promise((resolve, reject) => {
            let check_bookmark_sql = `select * from db_bookmark where user_id="${user_id}" and news_id="${news_id}"`;
            db.query(check_bookmark_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },

    //获取收藏列表
    getBookmarkList({ user_id }) {
        return new Promise((resolve, reject) => {
            let check_bookmark_sql = `select * from db_bookmark where user_id="${user_id}"`;
            db.query(check_bookmark_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },
};
